<?php

namespace app\admin\listener\log;

class LengthFilter implements FilterInterface
{
    protected int $maxLength;
    protected string $ellipsis;

    public function __construct()
    {
        $this->maxLength = config('filter.max_string_length', 250);
        $this->ellipsis = config('filter.ellipsis', '...');
    }

    public function apply(array &$data): void
    {
        foreach ($data as &$value) {
            if (is_string($value) && mb_strlen($value, 'UTF-8') > $this->maxLength) {
                $value = mb_substr($value, 0, $this->maxLength, 'UTF-8') . $this->ellipsis;
            }
        }
        unset($value);
    }
}